package com.yayo.mangement.filter.aop;

import com.yayo.mangement.utils.IOStreamUtils;
import com.yayo.mangement.resp.WorkbookResp;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;

//@Component
@Aspect
@Slf4j
public class ExportAop {

    @Pointcut("execution(public * com.yayo.mangement.rest.XlsxExporterController.export(..))")
    public void export(){}

    @AfterReturning(value = "export()", returning = "result")
    public void after(JoinPoint joinPoint, Object result){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        log.info(joinPoint.getSignature().getName());
        WorkbookResp workbook = (WorkbookResp) result;
        if(workbook != null){
            IOStreamUtils.output(workbook.getFileName(),workbook.getWorkbook(), response);
        }
    }
}
